<script>
export default {
  name: "ai",
  data() {
    return {
      inputMessage: '',
      messageList: [
      ]
    }
  },
  methods: {
    sendMessage() {
      if (!this.inputMessage.trim()) return;

      // 添加用户消息
      this.messageList.push({
        content: this.inputMessage,
        isUser: true
      });

      // 模拟AI回复
      setTimeout(() => {
        this.messageList.push({
          content: this.getAiResponse(),
          isUser: false
        });

        // 滚动到底部
        this.$nextTick(() => {
          this.$refs.chatContainer.scrollTop = this.$refs.chatContainer.scrollHeight;
        });
      }, 800);

      this.inputMessage = '';
    },
    getAiResponse() {
      // 这里可以扩展更多回复逻辑
      const responses = [
        '这是一个模拟回复',
        '我正在学习人类语言',
        '这个问题很有趣',
        '能再说详细些吗？'
      ];
      return responses[Math.floor(Math.random() * responses.length)];
    }
  }
}
</script>

<template>
  <div style="padding: 20px 25%; background-color: #F8F8F8FF; ">
    <el-card style="height: 85vh;">
      <div ref="chatContainer" style="height: 60vh; overflow-y: auto; padding: 20px">
        <div
          v-for="(msg, index) in messageList"
          :key="index"
          :style="{
            display: 'flex',
            justifyContent: msg.isUser ? 'flex-end' : 'flex-start',
            margin: '10px 0'
          }"
        >
          <div
            :style="{
              maxWidth: '70%',
              padding: '10px 15px',
              borderRadius: msg.isUser ? '15px 15px 0 15px' : '15px 15px 15px 0',
              backgroundColor: msg.isUser ? '#409EFF' : '#EBEEF5',
              color: msg.isUser ? 'white' : '#303133'
            }"
          >
            {{ msg.content }}
          </div>
        </div>
      </div>

      <div style="display: flex; margin-top: 20px">
        <el-input
          v-model="inputMessage"
          placeholder="输入消息..."
          @keyup.enter.native="sendMessage"
        />
        <el-button
          type="primary"
          style="margin-left: 10px"
          @click="sendMessage"
        >
          发送
        </el-button>
      </div>
    </el-card>
  </div>
</template>

<style scoped lang="scss">
/* 可以添加过渡动画 */
.el-card {
  transition: all 0.3s;
}

/* 滚动条样式 */
::-webkit-scrollbar {
  width: 6px;
}

::-webkit-scrollbar-track {
  background: #f1f1f1;
}

::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 4px;
}
</style>
